home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / ORBMECHD / ORBMECH2.C < prev    next >
C/C++ Source or Header  |  1991-06-26  |  10KB  |  485 lines

  1. #include    "SANE.h"
  2. #include    "orbmech.h"
  3.  
  4. Str255            gFileName;
  5. static     int        theVRefNum;
  6. int                 linesInFolder;
  7.  
  8. extern    WindowPtr        myWindow;
  9. extern    ControlHandle     vScroll;
  10. extern    TEHandle        TEH;
  11. extern    int                dirty;                    
  12. extern     WindowRecord    wRecord;
  13. extern    Boolean            gDone, gWriting;
  14. extern    DialogPtr        LambertDia, HohmannDia, HillsDia, KeplerDia, whichDialog, gHelpDia;
  15. extern    DialogPtr        gLamHelpDia, gHillsHelpDia, gKepHelpDia, gHohHelpDia;
  16. extern    extended        gParameter;
  17. extern  Cursor            gEditCursor;
  18.  
  19.  
  20.  
  21. /**********************/
  22.  
  23. DlgInits()
  24. {
  25.     LambertDia = GetNewDialog( LAMBERT_ID, NIL_POINTER, 0L );
  26.     if ( LambertDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
  27.  
  28.     HohmannDia = GetNewDialog( HOHMANN_ID, NIL_POINTER, 0L );
  29.     if ( HohmannDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
  30.  
  31.     HillsDia = GetNewDialog( HILLS_ID, NIL_POINTER, 0L );
  32.     if ( HillsDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
  33.  
  34.     KeplerDia = GetNewDialog( KEPLER_ID, NIL_POINTER, 0L );
  35.     if ( KeplerDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
  36.  
  37.     gHelpDia = GetNewDialog( HELP_DIA_ID, NIL_POINTER, 0L );
  38.     if ( gHelpDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
  39.  
  40.     gLamHelpDia = GetNewDialog( LAM_H_DIA_ID, NIL_POINTER, 0L );
  41.     if ( gLamHelpDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
  42.  
  43.     gHillsHelpDia = GetNewDialog( HILLS_H_DIA_ID, NIL_POINTER, 0L );
  44.     if ( gHillsHelpDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
  45.  
  46.     gKepHelpDia = GetNewDialog( KEP_H_DIA_ID, NIL_POINTER, 0L );
  47.     if ( gKepHelpDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
  48.  
  49.     gHohHelpDia = GetNewDialog( HOH_H_DIA_ID, NIL_POINTER, 0L );
  50.     if ( gHohHelpDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
  51.  
  52. }
  53.  
  54. /*************************/
  55.  
  56. SetUpFiles()
  57. {
  58.     pStrCopy("\p", gFileName);
  59.     theVRefNum = 0;
  60. }
  61.  
  62. /***************************/
  63.  
  64. WindowInit()
  65. {
  66.     Rect        destRect, viewRect;
  67.     Rect        vScrollRect;
  68.     FontInfo    myInfo;
  69.     int            height;
  70.     
  71.     myWindow = GetNewWindow( WINDOW_ID, &wRecord, -1L );
  72.     if ( myWindow == NIL_POINTER )  Alert( MSNG_RSRC_ALERT, 0L );
  73.     SetPort( myWindow );
  74.     
  75.     TextFont(4);
  76.     TextSize(9);
  77.     vScrollRect = (*myWindow).portRect;
  78.     vScrollRect.left = vScrollRect.right-15;
  79.     vScrollRect.right += 1;
  80.     vScrollRect.bottom -= 14;
  81.     vScrollRect.top -= 1;
  82.     vScroll = NewControl( myWindow, &vScrollRect, "\p", 1, 0, 0, 0,
  83.         scrollBarProc, 0L);
  84.  
  85.     viewRect = thePort->portRect;
  86.     viewRect.right -= S_BAR_WIDTH;
  87.     viewRect.bottom -= S_BAR_WIDTH;
  88.     InsetRect(&viewRect, 4, 4);
  89.     TEH = TENew( &viewRect, &viewRect );
  90.     SetView(thePort);
  91. }
  92.  
  93. /********************************/
  94.  
  95. MyGrowWindow( w, p )
  96. WindowPtr w;
  97. Point p;
  98. {
  99.     GrafPtr    savePort;
  100.     long    theResult;
  101.     int        oScroll;
  102.     Rect     r, oView;
  103.     
  104.     GetPort( &savePort );
  105.     SetPort( w );
  106.  
  107.     SetRect(&r, 80, 80, screenBits.bounds.right, screenBits.bounds.bottom);
  108.     theResult = GrowWindow( w, p, &r );
  109.     if (theResult == 0)
  110.       return;
  111.     SizeWindow( w, LoWord(theResult), HiWord(theResult), 1);
  112.  
  113.     InvalRect(&w->portRect);
  114.     oView = (**TEH).viewRect;
  115.     oScroll = GetCtlValue(vScroll);
  116.     
  117.     SetView(w);
  118.     HidePen();
  119.     MoveControl(vScroll, w->portRect.right - S_BAR_WIDTH, w->portRect.top-1);
  120.     SizeControl(vScroll, S_BAR_WIDTH+1, w->portRect.bottom - w->portRect.top-(S_BAR_WIDTH+1-2));
  121.     ShowPen();
  122.  
  123.  
  124.     SetVScroll();
  125.     AdjustText();
  126.     
  127.     SetPort( savePort );
  128. }
  129.  
  130. /*****************************/
  131.  
  132. ShowSelect()
  133.  
  134. {
  135.     register    int        topLine, bottomLine, theLine;
  136.     
  137.     SetVScroll();
  138.     AdjustText();
  139.     
  140.     topLine = GetCtlValue(vScroll);
  141.     bottomLine = topLine + linesInFolder;
  142.     
  143.     if ((**TEH).selStart < (**TEH).lineStarts[topLine] ||
  144.             (**TEH).selStart >= (**TEH).lineStarts[bottomLine]) {
  145.         for (theLine = 0; (**TEH).selStart >= (**TEH).lineStarts[theLine]; theLine++)
  146.             ;
  147.         SetCtlValue(vScroll, theLine - linesInFolder / 2);
  148.         AdjustText();
  149.     }
  150. }
  151.  
  152. /***************************/
  153.  
  154. UpdateWindow(theWindow)
  155. WindowPtr    theWindow;
  156. {
  157.     GrafPtr    savePort;
  158.     
  159.     GetPort( &savePort );
  160.     SetPort( theWindow );
  161.     BeginUpdate( theWindow );
  162.     EraseRect(&theWindow->portRect);
  163.     DrawControls( theWindow );
  164.     DrawGrowIcon( theWindow );
  165.     TEUpdate( &theWindow->portRect, TEH );
  166.     EndUpdate( theWindow );
  167.     SetPort( savePort );
  168. }
  169.  
  170. /*****************************/
  171.  
  172. AdjustText ()
  173.  
  174. {
  175.     int        oldScroll, newScroll, delta;
  176.     
  177.     oldScroll = (**TEH).viewRect.top - (**TEH).destRect.top;
  178.     newScroll = GetCtlValue(vScroll) * (**TEH).lineHeight;
  179.     delta = oldScroll - newScroll;
  180.     if (delta != 0)
  181.       TEScroll(0, delta, TEH);
  182. }
  183.  
  184. /****************************/
  185.  
  186. SetVScroll()
  187. {
  188.     register int    n;
  189.     
  190.     n = (**TEH).nLines-linesInFolder;
  191.  
  192.     if ((**TEH).teLength > 0 && (*((**TEH).hText))[(**TEH).teLength-1]=='\r')
  193.         n++;
  194.  
  195.     SetCtlMax(vScroll, n > 0 ? n : 0);
  196. }
  197.  
  198. /****************************/
  199.  
  200. SetView(w)
  201. WindowPtr w;
  202. {
  203.     (**TEH).viewRect = w->portRect;
  204.     (**TEH).viewRect.right -= S_BAR_WIDTH;
  205.     (**TEH).viewRect.bottom -= S_BAR_WIDTH;
  206.     InsetRect(&(**TEH).viewRect, 4, 4);
  207.     linesInFolder = ((**TEH).viewRect.bottom-(**TEH).viewRect.top)/(**TEH).lineHeight;
  208.     (**TEH).viewRect.bottom = (**TEH).viewRect.top + (**TEH).lineHeight*linesInFolder;
  209.     (**TEH).destRect.right = (**TEH).viewRect.right;
  210.     TECalText(TEH);
  211. }
  212.  
  213. /*******************/
  214.  
  215. int DoFile( item )
  216. int        item;
  217.  
  218. {
  219.     int     vRef, refNum, io;
  220.     Str255    fn;
  221.  
  222.     switch (item) {
  223.     case NEW_FILE: 
  224.         SetWTitle( myWindow, "\pUntitled");
  225.         ShowWindow( myWindow );
  226.         SelectWindow( myWindow );
  227.         break;
  228.     case OPEN_FILE:
  229.         if (OldFile(fn, &vRef ))
  230.             if (FSOpen(fn, vRef, &refNum)==noErr) {
  231.                 if (ReadFile( refNum, TEH )) {
  232.                     pStrCopy(fn, gFileName);
  233.                     theVRefNum = vRef;
  234.                     SetWTitle(myWindow, gFileName);
  235.                     dirty = 0;
  236.                 }
  237.                 if (FSClose(refNum)==noErr) ;
  238.                 ShowWindow( myWindow );
  239.                 SelectWindow( myWindow );
  240.                 TESetSelect(0, 0, TEH);
  241.                 ShowSelect();
  242.             }
  243.             else FileError( "\pError opening ", fn );
  244.         break;
  245.     case CLOSE_FILE:
  246.         if (dirty) {
  247.             ParamText("\pSave changes for ╥",
  248.                     (gFileName[0]==0) ? "\pUntitled" : (char *)gFileName,
  249.                     "\p╙?", "\p");
  250.             switch ( Alert(PARAM_3, 0L) ) {
  251.             case AA_SAVE:
  252.                 if (gFileName[0]==0) {
  253.                     fn[0] = 0;
  254.                     if (!SaveAs(fn, &vRef)) return(0);
  255.                 }
  256.                  else if (!SaveFile( gFileName, theVRefNum )) return(0);
  257.                  break;
  258.              case AA_CANCEL: return(0);
  259.              case AA_DISCARD: dirty = 0;
  260.              }
  261.          }
  262.         CloseMyWindow();
  263.         dirty = 0;
  264.         break;
  265.     case SAVE_FILE:
  266.         if (gFileName[0]==0) goto saveas;
  267.         SaveFile(gFileName, theVRefNum);
  268.         break;
  269.     case SAVE_AS:
  270.         saveas:
  271.         fn[0] = 0;
  272.         if (SaveAs(fn, &vRef )) {
  273.             pStrCopy(fn, gFileName);
  274.             theVRefNum = vRef;
  275.             SetWTitle(myWindow, gFileName);
  276.         }
  277.         break;
  278.     case REVERT_FILE:
  279.         ParamText("\pRevert to last saved version of ╥",
  280.                 gFileName, "\p╙?", "\p");
  281.         switch (Alert(PARAM_3, 0L)) {
  282.         case AA_SAVE:
  283.             HidePen();
  284.             TESetSelect( 0, (**TEH).teLength, TEH );
  285.             ShowPen();
  286.             TEDelete(TEH);
  287.             if ((gFileName[0]!=0) &&
  288.                 (FSOpen(gFileName, theVRefNum, &refNum)==noErr)) {
  289.                 dirty = !ReadFile( refNum, TEH ); 
  290.                 /* I don't check for bad read! */
  291.                 if (FSClose(refNum)==noErr) ;
  292.             }
  293.             ShowWindow( myWindow );
  294.             SelectWindow( myWindow );
  295.             UpdateWindow( myWindow );
  296.          case AA_CANCEL:
  297.          case AA_DISCARD: return(0);;
  298.          }
  299.  
  300.         break;
  301.         
  302.     /* insert print f */
  303.     
  304.     case PAGE_SETUP:
  305.         DoPageSetUp();
  306.         break;
  307.     case PRINT_FILE:
  308.         CompactMem( 100 );
  309.         PrintText( (**TEH).hText, (long)(**TEH).teLength, (GrafPtr)myWindow,
  310.                         StringWidth("\pmmmm"), 0L);
  311.         InitCursor();
  312.         break;
  313.     
  314.     /* end print f */
  315.     case QUIT_APPL: 
  316.         gDone = 1;
  317.         if (DoFile(CLOSE_FILE)) ExitToShell();
  318.     }
  319.     return(1);
  320. }
  321.  
  322. /*****************************/
  323.  
  324. pStrCopy( p1, p2 )
  325. register char *p1, *p2;
  326. /* copies a pascal string from p1 to p2 */
  327. {
  328.     register int len;
  329.     
  330.     len = *p2++ = *p1++;
  331.     while (--len>=0) *p2++=*p1++;
  332. }
  333.  
  334. /*************************************/
  335.  
  336. static Point SFGwhere = { 90, 82 };
  337. static Point SFPwhere = { 106, 104 };
  338. static SFReply reply;
  339.  
  340. SaveAs( fn, vRef )
  341. Str255     fn;
  342. int        *vRef;
  343. {
  344.     int refNum;
  345.     
  346.     if (NewFile(fn, vRef)) 
  347.         if (CreateFile(fn, vRef, &refNum)) {
  348.             WriteFile(refNum, (*(**TEH).hText), (long)(**TEH).teLength);
  349.             FSClose( refNum );
  350.             dirty = 0;
  351.             return(1);
  352.         }
  353.         else {
  354.             FileError("\pError creating file ", fn);
  355.         }
  356.     return(0);
  357. }
  358.  
  359. /*******************************/
  360.  
  361. SaveFile( fn, vrn )
  362. Str255    fn;
  363. int        vrn;
  364. {
  365.     int refNum;
  366.     
  367.     if (FSOpen( fn, vrn, &refNum )==noErr) {
  368.         WriteFile(refNum, (*(**TEH).hText), (long)(**TEH).teLength);
  369.         dirty = 0;
  370.         FSClose( refNum );
  371.         return(1);
  372.     }
  373.     else FileError("\pError opening file ", fn);
  374.     return(0);
  375. }
  376.  
  377. /*******************************/
  378.  
  379.  
  380. NewFile( fn, vRef )
  381. Str255    fn;
  382. int        *vRef;
  383. {
  384.     SFPutFile(SFPwhere, "\p", fn, 0L, &reply);
  385.     if (reply.good) {
  386.         pStrCopy(reply.fName, fn);
  387.         *vRef = reply.vRefNum;
  388.         gWriting = 1;
  389.         return(1);
  390.     }
  391.     else return(0);
  392. }
  393.  
  394. /*******************************/
  395.  
  396. OldFile( fn, vRef )
  397. Str255    fn;
  398. int        *vRef;
  399. {
  400.     SFTypeList    myTypes;
  401.     
  402.     myTypes[0]='TEXT';
  403.     SFGetFile( SFGwhere, "\p", 0L, 1, myTypes, 0L, &reply );
  404.     if (reply.good) {
  405.         pStrCopy( reply.fName, fn );
  406.         *vRef = reply.vRefNum;
  407.         gWriting = 1;
  408.         return(1);
  409.     }
  410.     else return(0);
  411. }
  412.  
  413.  
  414. /*******************************/
  415.  
  416. CreateFile( fn, vRef, theRef )
  417. Str255    fn;
  418. int        *vRef;
  419. int        *theRef;
  420. {
  421.     int io;
  422.     
  423.     io=Create(fn, *vRef, 'orbM', 'TEXT');
  424.     if ((io==noErr) || (io==dupFNErr)) io = FSOpen( fn, *vRef, theRef );
  425.     return( (io==noErr) || (io=dupFNErr) );
  426. }
  427.  
  428.  
  429. /*******************************/
  430.  
  431. WriteFile( refNum, p, num )
  432. int        refNum;
  433. char    *p;
  434. long    num;
  435. {
  436.     int io;            /*     a real application would want to check 
  437.                         this return code for failures */
  438.     
  439.     io=FSWrite( refNum, &num, p );
  440. }
  441.  
  442.  
  443. /*******************************/
  444.  
  445. ReadFile( refNum, textH )
  446. int        refNum;
  447. TEHandle textH;
  448. {
  449.     char    buffer[256];
  450.     long    count;
  451.     int        io;
  452.     
  453.     TESetSelect(0, (**textH).teLength, textH);
  454.     TEDelete( textH );
  455.     do {
  456.         count = 256;
  457.         io = FSRead( refNum, &count, &buffer );
  458.         TEInsert( &buffer, count, textH );
  459.     } while (io==noErr);
  460.     
  461.     return( io==eofErr );
  462. }
  463.  
  464.  
  465. /*******************************/
  466.  
  467. FileError( s, f )
  468. Str255 s, f;
  469. {
  470.     ParamText(s, f,"\p", "\p");
  471.     Alert( FILE_ERROR_ALERT, 0L );
  472. }
  473.  
  474. /********************************/
  475.  
  476. CloseMyWindow()
  477. {
  478.     HideWindow( myWindow );
  479.     TESetSelect( 0, (**TEH).teLength, TEH );
  480.     TEDelete( TEH );
  481.     SetVScroll();
  482.     SetUpFiles();
  483.     gWriting = 0;
  484. }
  485.